import express from 'express'
export const router = express.Router()
import { createConnectionPool } from '../../mysql/index.js'
import * as PageUtil from '../../utils/pageutil.js'

const connection = await createConnectionPool()
function FormCheck(form) {
    const { pageSize , pageNum } = form
    if (!pageSize) {
        return {
            err: true,
            message: 'pageSize不能为空'
        }
    }
    if (!pageNum) {
        return {
            err: true,
            message: 'pageNum不能为空'
        }
    }
}
router.get("/interface/page", async (req, res) => {
    try {
        const form = req.query
        const verify = FormCheck(form)
        if (verify && verify.err) {
            res.status(500).json({ error: verify.message })
            return
        }
        const { pageSize = 10, pageNum = 1 } = req.query
        const countSql = "select count(*) as total from interface where deleted = 0"
        const [countRows, countFields] = await connection.execute(countSql)
        const total = countRows[0].total
        const page = PageUtil.toPage(total, pageSize, pageNum)
        console.log(page,'pagepage');
        let data = []
        if (page.hasVal) {
            const sql = `select * from interface where deleted = 0 order by create_time desc limit ${page.limit},${page.pageSize}`
            const [rows, fields] = await connection.execute(sql)
            data = rows
        }
        res.status(200).send({ code: 200, message: '获取数据成功', data, total })
    } catch (err) {
        console.error('Error fetching user:', err)
        res.status(500).json({ error: '获取数据失败' })
    }
})

function verifyForm(form) {
    const { name, value_1, value_2, value_3, value_4, value_5, key_name } = form
    if (!name) {
        return {
            err: true,
            message: '接口名不能为空'
        }
    }
    if (!value_1) {
        return {
            err: true,
            message: '接口值1不能为空'
        }
    }
    if (!value_2) {
        return {
            err: true,
            message: '接口值2不能为空'
        }
    }
    if (!value_3) {
        return {
            err: true,
            message: '接口值3不能为空'
        }
    }
    if (!value_4) {
        return {
            err: true,
            message: '接口值4不能为空'
        }
    }
    if (!value_5) {
        return {
            err: true,
            message: '接口值5不能为空'
        }
    }
    if (!key_name) {
        return {
            err: true,
            message: '键名不能为空'
        }
    }

    return {
        err: false,
        message: ''
    }
}

router.post("/interface/add", async (req, res) => {
    try {
        const form = req.body
        const verify = verifyForm(form)
        if (verify.err) {
            res.status(500).json({ error: verify.message })
            return
        }
        const sql = "insert into interface (name, value_1, value_2, value_3, value_4, value_5, key_name, remarks, create_time, deleted) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
        await connection.execute(sql, [form.name || null, form.value_1 || null, form.value_2 || null, form.value_3 || null, form.value_4 || null, form.value_5 || null, form.key_name || null, form.remarks || null, new Date(), 0])
        res.status(200).send({ code: 200, message: '新增数据成功' })
    } catch (err) {
        console.error('Error adding user:', err)
        res.status(500).json({ error: '新增数据失败' })
    }
})

router.post("/interface/update", async (req, res) => {
    try {
        const form = req.body
        if (!form.id) {
            res.status(500).json({ error: 'id不能为空' })
            return
        }
        const verify = verifyForm(form)
        if (verify.err) {
            res.status(500).json({ error: verify.message })
            return
        }
        const sql = "update interface set name = ?, value_1 = ?, value_2 = ?, value_3 = ?, value_4 = ?, value_5 = ?, key_name = ?, remarks = ? where id = ?"
        await connection.execute(sql, [form.name || null, form.value_1 || null, form.value_2 || null, form.value_3 || null, form.value_4 || null, form.value_5 || null, form.key_name || null, form.remarks || null, form.id])
        res.status(200).send({ code: 200, message: '更新数据成功' })
    } catch (err) {
        console.error('Error updating user:', err)
        res.status(500).json({ error: '更新数据失败' })
    }
})

router.delete("/interface/delete", async (req, res) => {
    try {
        const { id } = req.query
        if (!id) {
            res.status(500).json({ error: 'id不能为空' })
            return
        }
        const sql = "update interface set deleted = 1 where id = ?"
        await connection.execute(sql, [id])
        res.status(200).send({ code: 200, message: '删除数据成功' })
    } catch (err) {
        console.error('Error deleting user:', err)
        res.status(500).json({ error: '删除数据失败' })
    }
})

export default router;